package {
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.text.StaticText;
	import flash.utils.Timer;
	import com.combiform.core.*;
	import com.combiform.harness.Combi_Harness;
	
	public class Main extends MovieClip {
		//globals
		const WIDTH:int=600, HEIGHT:int=300;
		
		
		//api
		var ioHandler:myIO_Handler;
		var cHarness:Combi_Harness;
		var co:Combine_Obj;
		var debug:Boolean=false;

		//game code
		public var creatures:Array;
		
		public var bubbles:Array;
		
		public var crabs:Array;
		
		public static var TIME:Number = 0;
		public static var TIME_DIFF:Number = 1;
		
		var gameTimer:Timer;
		
		public function Main() {
			creatures = [];
			bubbles = [];
			crabs = [];
			
			this.addEventListener(Event.ADDED_TO_STAGE, init);
			
			gameTimer = new Timer(33, 0);
			gameTimer.start();
			gameTimer.addEventListener(TimerEvent.TIMER, onTimer);
		}
		
		function init(e:Event){
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			stop();
			//var t:Number = getTimer();
			
			
			//Combiform api ** note that in the released version you probably want to take out the combiharness
			ioHandler = new myIO_Handler(this.stage,true,false);
//			cHarness= new Combi_Harness(this.stage);
//			addChild(cHarness);
			co = new Combine_Obj();
			
			//listeners
//			cHarness.addEventListener(IR_Event.INPUT, irHandler);
			ioHandler.addEventListener(IR_Event.INPUT, irHandler);
			
			//addEventListener(Event.ENTER_FRAME, onFrame);
			
			//test
			
			var crab = new Crab();
			
			
			addChild(crab);
			
			
			crab.x = 400;
			crab.y = 450;
			
			crabs.push(crab);
			
			this.addEventListener(CustomEvent.SHOOT_BUBBLE, shootBubble, true);
			
		}

		//main loop: ioHandler needs to call frameH so make sure this is in your main loop
		function onTimer(e:TimerEvent) : void {
			
			var t:Number = gameTimer.currentCount;
			
			
			TIME_DIFF = (t - TIME) / 1000; // Finds the time since the last frame in seconds
			TIME = t; // Stores the current milliseconds in the TIME variable
			
			
			ioHandler.frameH();
			for (var i in creatures) {
				(creatures[i] as Creature).frameH(e);
			}
			for (var i in bubbles) {
				(bubbles[i] as Bubble).frameH(e);
			}
			
			for (var i in crabs) {
				(crabs[i] as Crab).frameH(e);
			}
			
		}
		
		function shootBubble(e:CustomEvent) {
			
			
			var bubble = new Bubble(e.target.Angle);
			addChild(bubble);
			bubbles.push(bubble);
			
			bubble.x = e.target.x + e.target.width / 2;
			
			
			bubble.y = e.target.y;		
			
		}
      
		
      //irHandler is how your game will process IR_Events
		function irHandler(e:IR_Event):void{
			switch(e.key){
				case IR_Event.IR_ACTIONBUTTON:
					trace ("Main:"+e.controllerNum+":KNOB e.IR_ACTIONBUTTON val="+e.val)
					
					break;
				case IR_Event.IR_LEFTBUTTON:
//					trace ("Main:e.IR_LEFTBUTTON val="+e.val)
					break;
				case IR_Event.IR_RIGHTBUTTON:
//					trace ("Main:e.IR_RIGHTBUTTON val="+e.val)
					break;
				case IR_Event.IR_DOWNBUTTON:
//					trace ("Main:e.IR_DOWNBUTTON val="+e.val)
					break;
				case IR_Event.IR_UPBUTTON:
//					trace ("Main:e.IR_UPBUTTON val="+e.val)
					break;
				case IR_Event.IR_PRECOMBINEBUTTON:
//					trace ("Main:e.IR_PRECOMBINEBUTTON val="+e.val)
					
					break;
				case IR_Event.IR_LED:
				case IR_Event.IR_LED1:
				case IR_Event.IR_LED2:
				case IR_Event.IR_LED3:
					ioHandler.sendEvent(e);
					trace("Main sent LED event");
					break;
				case IR_Event.IR_COMBINEBUTTON:
					trace ("Main:e.IR_COMBINEBUTTON")
					e.combine_obj.printComb_obj();
					co=e.combine_obj.clone();
					break;
				default: break;
			}
		
			var connected:Boolean=false;
			
			//if (debug) print_score();
		}
		
	}
}